﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace Zjh.PackSerializer.Converters.Base
{
    /// <summary>
    /// 枚举类型转换器
    /// </summary>
    public class EnumConverter : PackConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType.IsEnum;
        }

        public override bool Read(Stream stream, Type objectType, IPackSerializer serializer, out object output)
        {
            output = null;
            var valueType = ((FieldInfo[])((TypeInfo)objectType).DeclaredFields)[0].FieldType;
            object value = null;
            if (valueType.Equals(typeof(byte)))
            {
                value = (byte)serializer.Deserialize(stream, valueType);
            }
            else if (valueType.Equals(typeof(short)))
            {
                value = (short)serializer.Deserialize(stream, valueType);
            }
            else if (valueType.Equals(typeof(int)))
            {
                value = (int)serializer.Deserialize(stream, valueType);
            }
            else if (valueType.Equals(typeof(long)))
            {
                value = (long)serializer.Deserialize(stream, valueType);
            }
            else if (valueType.Equals(typeof(ushort)))
            {
                value = (ushort)serializer.Deserialize(stream, valueType);
            }
            else if (valueType.Equals(typeof(uint)))
            {
                value = (uint)serializer.Deserialize(stream, valueType);
            }
            else if (valueType.Equals(typeof(ulong)))
            {
                value = (ulong)serializer.Deserialize(stream, valueType);
            }
            if (value == null)
            {
                return false;
            }
            output = Enum.Parse(objectType, value.ToString());
            return true;
        }

        public override bool Write(Stream stream, Type objectType, object input, IPackSerializer serializer)
        {
            var valueType = ((FieldInfo[])((TypeInfo)objectType).DeclaredFields)[0].FieldType;
            if (valueType.Equals(typeof(byte)))
            {
                serializer.Serialize(stream, valueType, (byte)input);
            }
            else if (valueType.Equals(typeof(short)))
            {
                serializer.Serialize(stream, valueType, (short)input);
            }
            else if (valueType.Equals(typeof(int)))
            {
                serializer.Serialize(stream, valueType, (int)input);
            }
            else if (valueType.Equals(typeof(long)))
            {
                serializer.Serialize(stream, valueType, (long)input);
            }
            else if (valueType.Equals(typeof(ushort)))
            {
                serializer.Serialize(stream, valueType, (ushort)input);
            }
            else if (valueType.Equals(typeof(uint)))
            {
                serializer.Serialize(stream, valueType, (uint)input);
            }
            else if (valueType.Equals(typeof(ulong)))
            {
                serializer.Serialize(stream, valueType, (ulong)input);
            }
            else
            {
                return false;
            }
            return true;
        }
    }
}
